var linesdata = [];
// /初始化echarts,并和框体map绑定。
var myChart = echarts.init(document.getElementById('map'));
//echarts中使用地图的配置参数
var planePath = 'path://M1705.06,1318.313v-89.254l-319.9-221.799l0.073-208.063c0.521-84.662-26.629-121.796-63.961-121.491c-37.332-0.305-64.482,36.829-63.961,121.491l0.073,208.063l-319.9,221.799v89.254l330.343-157.288l12.238,241.308l-134.449,92.931l0.531,42.034l175.125-42.917l175.125,42.917l0.531-42.034l-134.449-92.931l12.238-241.308L1705.06,1318.313z';
var greenLine = 'path://M50.45,520C30.16,354.74-0.27,112.65-0.27,60.46c0-81.5,100-79.73,100,0C99.73,117,75.09,356.19,50.45,520z'
var option = {
    bmap: {
        // 百度地图中心经纬度 坐标拾取器http://api.map.baidu.com/lbsapi/getpoint/index.html
        center: [108.970631,34.248439],
        name: '中铁一局信息中心',
        // 百度地图缩放等级，数字越大，放大越大，地图比例尺越小
        zoom:6,
        // 是否开启拖拽缩放，可以只设置 'scale' 或者 'move'
        roam: true,
        scale: true,
        move: true,
        // mapStyle是百度地图的自定义样式，见 http://developer.baidu.com/map/custom/
        mapStyle: {
            styleJson:[
                {
                    "featureType": "water",
                    "elementType": "all",
                    "stylers": {
                        "color": "#021019"
                    }
                },
                {
                    "featureType": "highway",
                    "elementType": "geometry.fill",
                    "stylers": {
                        "color": "#000000"
                    }
                },
                {
                    "featureType": "highway",
                    "elementType": "geometry.stroke",
                    "stylers": {
                        "color": "#147a92"
                    }
                },
                {
                    "featureType": "arterial",
                    "elementType": "geometry.fill",
                    "stylers": {
                        "color": "#000000"
                    }
                },
                {
                    "featureType": "arterial",
                    "elementType": "geometry.stroke",
                    "stylers": {
                        "color": "#0b3d51"
                    }
                },
                {
                    "featureType": "local",
                    "elementType": "geometry",
                    "stylers": {
                        "color": "#000000"
                    }
                },
                {
                    "featureType": "land",
                    "elementType": "all",
                    "stylers": {
                        "color": "#08304b"
                    }
                },
                {
                    "featureType": "railway",
                    "elementType": "geometry.fill",
                    "stylers": {
                        "color": "#000000"
                    }
                },
                {
                    "featureType": "railway",
                    "elementType": "geometry.stroke",
                    "stylers": {
                        "color": "#08304b"
                    }
                },
                {
                    "featureType": "subway",
                    "elementType": "geometry",
                    "stylers": {
                        "lightness": -70
                    }
                },
                {
                    "featureType": "building",
                    "elementType": "geometry.fill",
                    "stylers": {
                        "color": "#000000"
                    }
                },
                {
                    "featureType": "all",
                    "elementType": "labels.text.fill",
                    "stylers": {
                        "color": "#857f7f"
                    }
                },
                {
                    "featureType": "all",
                    "elementType": "labels.text.stroke",
                    "stylers": {
                        "color": "#000000"
                    }
                },
                {
                    "featureType": "building",
                    "elementType": "geometry",
                    "stylers": {
                        "color": "#022338"
                    }
                },
                {
                    "featureType": "green",
                    "elementType": "geometry",
                    "stylers": {
                        "color": "#062032"
                    }
                },
                {
                    "featureType": "boundary",
                    "elementType": "all",
                    "stylers": {
                        "color": "#1afeffff"
                    }
                },
                {
                    "featureType": "manmade",
                    "elementType": "geometry",
                    "stylers": {
                        "color": "#022338"
                    }
                },
                {
                    "featureType": "poi",
                    "elementType": "all",
                    "stylers": {
                        "visibility": "off"
                    }
                },
                {
                    "featureType": "all",
                    "elementType": "labels.icon",
                    "stylers": {
                        "visibility": "off"
                    }
                },
                {
                    "featureType": "all",
                    "elementType": "labels.text.fill",
                    "stylers": {
                        "color": "#00ffffff",
                        "hue": "#00ffff",
                        "visibility": "on"
                    }
                }
            ]
        }
    },
    //series是在地图上的线条等效果的配置文件，具体可以查阅文档。
    series: [{
        type: 'lines',
        coordinateSystem: 'bmap',
        zlevel: 2,
        symbolSize: 10,
        effect: {
            show: true,
            period: 6,
            trailLength: 0,
            symbol: 'arrow',
            symbolSize: 10
        },
        lineStyle: {
            normal: {
                color: "#a6c84c",
                width: 2,
                opacity: 0.6,
                curveness: 0.2
            }
        },
        itemStyle: {
            normal: {
                color: "#FFFFFF",
            },
        },
        //将手动做的一个迁徙数据放入线条的数据部分。
        data: linesdata
    }]
};
//配置参数传入图形实例中
myChart.setOption(option);
//初始化bmap和echarts实例绑定
var bmap = myChart.getModel().getComponent('bmap').getBMap();
var iconCenter=  new BMap.Icon("../imgs/shujuzhongxin.png", new BMap.Size(120, 142));
var centerMarker = new BMap.Marker(new BMap.Point(108.970631,34.248439),{icon:iconCenter});// 创建标注
bmap.addOverlay(centerMarker);
//地图上添加标注点
var  projectData =[];
function projectMarker(type) {
    $.support.cors = true;
    $.ajax({
        url: 'http://localhost:8888/v1/collect/data/screen/getProjectData?type=' + type,
        type: 'GET',
        async: false,
        timeout: 30000,
        dataType: 'json',
        success: function(result, textStatus, xhr) {
            if (result.data) {
                projectData = result.data;
                // var markers = [];
                console.log(projectData)
                for(var i = 0;i<projectData.length;i++){
                    var icon1 ;
                    var proName =  projectData[i].proName;
                    //1:断开 0:正常
                    if(projectData[i].status.length>0){
                        var status = projectData[i].status.indexOf("1");
                        var style='';
                        if(status!=-1){
                            status = 1;
                        }
                        switch(status) {
                            //异常
                            case 1:
                                icon1 =  new BMap.Icon("../imgs/weizhi_red.png", new BMap.Size(44, 44));
                                style= {
                                    normal: {
                                        color: "#ff1800",
                                        width: 2,
                                        opacity: 0.85,
                                        curveness: -0.3
                                    }
                                }
                                break;
                            case -1:
                                icon1=  new BMap.Icon("../imgs/weizhi_lv.png", new BMap.Size(46, 46));
                                style= {
                                    normal: {
                                        color: "#00b121",
                                        width: 4,
                                        opacity: 0.85,
                                        curveness: -0.3
                                    }
                                }
                                break;
                            default:
                                icon1 =  new BMap.Icon("../imgs/weizhi_lv.png", new BMap.Size(46, 46));
                        }
                        var marker = new BMap.Marker(new BMap.Point(projectData[i].coords[0],projectData[i].coords[1]),{icon:icon1});// 创建标注
                        bmap.addOverlay(marker);
                        //设置项目名称标签
                        var label = new BMap.Label(proName,{offset:new BMap.Size(-20,-10)});
                        label.setStyle({ backgroundColor:"#022963",color : "#00fbfc",border:"1px solid #00fbfc",
                            padding:"5px 10px",opacity:"0.85", fontSize : "14px" });
                        marker.setLabel(label)
                        //注册点击事件
                        var pro ={
                            fromName: proName,
                            toName: "中铁一局信息中心",
                            coords: [
                                [parseFloat(projectData[i].coords[0]),parseFloat(projectData[i].coords[1])],
                                [108.970631,34.248439]
                            ],
                            lineStyle:style
                        }
                        linesdata.push(pro);
                    }
                }
            }
        },
        beforeSend: function(xhr) {
            xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
            xhr.setRequestHeader("Access-Control-Allow-Methods","GET, POST, PUT, DELETE, OPTIONS");
            xhr.setRequestHeader("ccess-Control-Request-Methods","GET, POST, PUT, DELETE, OPTIONS");
        },
        error: function(xhr, textStatus) {
            return null;
        },
        complete: function() {}
    })
}

function getCount(type){
    $.support.cors = true;
    $.ajax({
        url: 'http://localhost:8888/v1/collect/data/screen/getProjectCollectDataStatus?type=' + type,
        type: 'GET',
        async: false,
        timeout: 30000,
        dataType: 'json',
        success: function(result, textStatus, xhr) {
            if (result.data) {
                console.log(result.data);
                $("#totalonnnect").text(result.data.total)
                $("#normal").text(result.data.normal)
                $("#error").text(result.data.error)
            }
        },
        beforeSend: function(xhr) {
            xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
            xhr.setRequestHeader("Access-Control-Allow-Methods","GET, POST, PUT, DELETE, OPTIONS");
            xhr.setRequestHeader("ccess-Control-Request-Methods","GET, POST, PUT, DELETE, OPTIONS");
        },
        error: function(xhr, textStatus) {
            return null;
        },
        complete: function() {}
    })
}
function getErrorDev(type){
    $.support.cors = true;
    $.ajax({
        url: 'http://localhost:8888/v1/collect/data/screen/getProjectCollectDataErrorDev?type=' + type,
        type: 'GET',
        async: false,
        timeout: 30000,
        dataType: 'json',
        success: function(result, textStatus, xhr) {
            if (result.data) {
                var  tbmlist = result.data;
                var orderBody = '';
                $.each(tbmlist, function (index, val) {
                    orderBody += '<span onclick="moveTo('+val.position+')" >'+ val.proName+'->' + val.secName+'->' + val.lineName + '->"' +val.tbmName +'</span><br/>';
                })
                console.log(orderBody);
                $("#tbmList").html(orderBody);
            }
        },
        beforeSend: function(xhr) {
            xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
            xhr.setRequestHeader("Access-Control-Allow-Methods","GET, POST, PUT, DELETE, OPTIONS");
            xhr.setRequestHeader("ccess-Control-Request-Methods","GET, POST, PUT, DELETE, OPTIONS");
        },
        error: function(xhr, textStatus) {
            return null;
        },
        complete: function() {}
    })
}

bmap.addControl(new BMap.MapTypeControl({mapTypes:[]}));
//添加城市选择列表
var size = new BMap.Size(10, 20);
bmap.addControl(new BMap.CityListControl({
    anchor: BMAP_ANCHOR_TOP_LEFT,
    offset: size,
    // 切换城市之间事件
    onChangeBefore: function(){
    },
    // 切换城市之后事件
    onChangeAfter:function(){
    }
}));
//添加版权控件
var cr = new BMap.CopyrightControl({anchor: BMAP_ANCHOR_BOTTOM_RIGHT});   //设置版权控件位置
bmap.addControl(cr); //添加版权控件
var bs = bmap.getBounds();   //返回地图可视区域
cr.addCopyright({id: 1, content: "<a href='#' style='font-size:20px;background:yellow'>中铁一局集团公司智能科技分公司</a>", bounds: bs});
//在地图上标注项目地点
projectMarker(0);
$("#buildProject").text(projectData.length)
myChart.setOption(option);
getCount(0);
getErrorDev(0);
function showData(type){
    remove_overlay();

    myChart.clear();
    linesdata=[];
    myChart.setOption(option);
    bmap = myChart.getModel().getComponent('bmap').getBMap();
    projectMarker(type);
    option.series[0].data=linesdata;
    myChart.setOption(option);
    getCount(type);
    getErrorDev(type);
    var iconCenter=  new BMap.Icon("../imgs/shujuzhongxin.png", new BMap.Size(120, 142));
    var centerMarker = new BMap.Marker(new BMap.Point(108.970631,34.248439),{icon:iconCenter});// 创建标注
    bmap.addOverlay(centerMarker);
    $("#buildProject").text(projectData.length)
}
//清除覆盖物
function remove_overlay(){
    bmap.clearOverlays();
}
//项目跳转
function moveTo(lng,lat){
    bmap.centerAndZoom(new BMap.Point(lng,lat), 10);

}

